import axios from 'axios';
const MybaseURL = 'http://localhost:4000'; 
// axios.defaults.baseURL = 'http://localhost:4000'; 
// 利用封装ajax
export default function MyAjax(url, type="GET", data={}) {
    return new Promise((resolve, reject) => {
        let promise;
        if(type === 'GET') {
            // // 准备拼接请求参数
            let data_str = '';
            Object.keys(data).forEach((k) => {
                data_str += k + '=' + data[k] + '&'; //记住此时多一个&尾部
            });
            if(data_str != '') {
                data_str = data_str.substring(0, data_str.length-1)
                url = url + '?' + data_str;
            }
            promise =  axios.get(MybaseURL + url);
        }else {
            promise =  axios.post(MybaseURL + url, data);
        }

        promise.then((response) => {
            resolve(response.data)
        }, (error) => {
            reject(error)
        });
    });
}